home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 February / SGI IRIX 6.5 Complementary Applications 2004 February.iso / dist / cde.idb / usr / dt / share / examples / dtdnd / demo.c.z / demo.c
Encoding:
C/C++ Source or Header  |  2003-11-18  |  12.2 KB  |  515 lines

  1. /*
  2.  * demo.c
  3.  *
  4.  * Copyright 2000, Silicon Graphics, Inc.
  5.  * ALL RIGHTS RESERVED
  6.  * 
  7.  * UNPUBLISHED -- Rights reserved under the copyright laws of the United
  8.  * States.   Use of a copyright notice is precautionary only and does not
  9.  * imply publication or disclosure.
  10.  *
  11.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  12.  * Use, duplication or disclosure by the Government is subject to restrictions
  13.  * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  14.  * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  15.  * in similar or successor clauses in the FAR, or the DOD or NASA FAR
  16.  * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  17.  * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  18.  *
  19.  * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  20.  * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  21.  * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  22.  * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  23.  * GRAPHICS, INC.
  24.  */
  25. /* $XConsortium: demo.c /main/cde1_maint/1 1995/07/17 16:41:59 drk $ */
  26. /*****************************************************************************
  27.  *****************************************************************************
  28.  **
  29.  **   File:         demo.c
  30.  **
  31.  **   Description:  The Drag & Drop Demo program demonstrates the
  32.  **            CDE DnD functions in the Desktop Services library:
  33.  **          
  34.  **                DtDndDragStart.3x
  35.  **                DtDndDropRegister.3x
  36.  **                DtDndCreateSourceIcon.3x
  37.  **          
  38.  **            The demo consists of a row of three different sources
  39.  **            for text, filename and appointment buffer drags.
  40.  **            It also has a text field that can accept either
  41.  **            text or filename drops.  Finally there is a data
  42.  **            area that accepts filename or buffer drops.
  43.  **
  44.  **  (c) Copyright 1993, 1994 Hewlett-Packard Company
  45.  **  (c) Copyright 1993, 1994 International Business Machines Corp.
  46.  **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  47.  **  (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
  48.  **      Novell, Inc.
  49.  **
  50.  ****************************************************************************
  51.  ************************************<+>*************************************/
  52.  
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <unistd.h>
  56. #include <time.h>
  57.  
  58. #include <X11/Intrinsic.h>
  59.  
  60. #include <Xm/Xm.h>
  61. #include <Xm/DrawingA.h>
  62. #include <Xm/Frame.h>
  63. #include <Xm/MainW.h>
  64. #include <Xm/MwmUtil.h>
  65. #include <Xm/RowColumn.h>
  66. #include <Xm/Separator.h>
  67. #include <Xm/DragDrop.h>
  68. #include <Xm/Screen.h>
  69.  
  70. #include <Dt/Dt.h>
  71. #include <Dt/Dnd.h>
  72.  
  73. #include "icon.h"
  74. #include "text.h"
  75. #include "file.h"
  76. #include "buff.h"
  77.  
  78.  /*************************************************************************
  79.  *
  80.  *    Data Structures & Declarations For Drag & Drop Demo
  81.  *
  82.  *************************************************************************/
  83.  
  84. /*
  85.  * The Drag Threshold is the distance, measured in pixels, over which the
  86.  * pointer must travel while the BSelect button (first mouse button) is held
  87.  * down in order to start a drag. CDE defines this to be 10 pixels.
  88.  */
  89.  
  90. #define DRAG_THRESHOLD 10
  91.  
  92. /*
  93.  * Absolute value macro
  94.  */
  95.  
  96. #ifndef ABS
  97. #define ABS(x) (((x) > 0) ? (x) : (-(x)))
  98. #endif
  99.  
  100. /*
  101.  * Global variables
  102.  */
  103.  
  104. Widget        topLevel;
  105. XtAppContext    appContext;
  106. Boolean        doingDrag = False;
  107.  
  108. /*
  109.  * Private Drag & Drop Demo Function Declarations
  110.  */
  111.  
  112. void        demoTransferCallback(Widget, XtPointer, XtPointer);
  113.  
  114.  /*************************************************************************
  115.  *
  116.  *    General-Purpose Drag & Drop Functions
  117.  *
  118.  *************************************************************************/
  119.  
  120. /*
  121.  * demoDragFinishCallback
  122.  *
  123.  * Resets drag state to indicate the drag is over.
  124.  */
  125. void
  126. demoDragFinishCallback(
  127.     Widget        widget,
  128.     XtPointer    clientData,
  129.     XtPointer    callData)
  130. {
  131.     doingDrag = False;
  132. }
  133.  
  134. /*
  135.  * demoDragMotionHandler
  136.  *
  137.  * Determine if the pointer has moved beyond the drag threshold while button 1
  138.  * was being held down.
  139.  */
  140. void
  141. demoDragMotionHandler(
  142.     Widget        dragInitiator,
  143.     XtPointer    clientData,
  144.     XEvent           *event)
  145. {
  146.     static int    initialX = -1;
  147.     static int    initialY = -1;
  148.     int        diffX, diffY;
  149.     long        dragProtocol = (long)clientData;
  150.  
  151.     if (!doingDrag) {
  152.  
  153.         /*
  154.           * If the drag is just starting, set initial button down coords
  155.          */
  156.  
  157.         if (initialX == -1 && initialY == -1) {
  158.             initialX = event->xmotion.x;
  159.             initialY = event->xmotion.y;
  160.         }
  161.  
  162.         /*
  163.          * Find out how far pointer has moved since button press
  164.          */
  165.  
  166.         diffX = initialX - event->xmotion.x;
  167.         diffY = initialY - event->xmotion.y;
  168.         
  169.         if ((ABS(diffX) >= DRAG_THRESHOLD) ||
  170.             (ABS(diffY) >= DRAG_THRESHOLD)) {
  171.             doingDrag = True;
  172.             switch (dragProtocol) {
  173.             case DtDND_TEXT_TRANSFER:
  174.                 textDragStart(dragInitiator, event);
  175.                 break;
  176.             case DtDND_FILENAME_TRANSFER:
  177.                 fileCheckForDrag(dragInitiator, event,
  178.                     initialX, initialY);
  179.                 break;
  180.             case DtDND_BUFFER_TRANSFER:
  181.                 apptDragStart(dragInitiator, event);
  182.                 break;
  183.             }
  184.             initialX = -1;
  185.             initialY = -1;
  186.         }
  187.     }
  188. }
  189.  
  190. /*
  191.  * demoDrawAnimateCallback
  192.  *
  193.  * Expands the icon melted into the draw area by Motif.
  194.  */
  195. void
  196. demoDrawAnimateCallback(
  197.     Widget        dragContext, /* WARNING: This is being destroyed. */
  198.     XtPointer    clientData,
  199.     XtPointer    callData)
  200. {
  201.     DtDndDropAnimateCallbackStruct *animateInfo =
  202.                 (DtDndDropAnimateCallbackStruct *) callData;
  203.     Widget        dropDraw = (Widget)clientData;
  204.     Display         *display = XtDisplayOfObject(dropDraw);
  205.     Screen            *screen    = XtScreen(dropDraw);
  206.     Window          window = XtWindow(dropDraw);
  207.     int        expandWidth, expandHeight,
  208.             sourceX, sourceY;
  209.     static GC    graphicsContext = NULL;
  210.     XGCValues    gcValues;
  211.     IconInfo        *iconPtr;
  212.     
  213.     /*
  214.      * Create graphics context if it doesn't yet exist
  215.      */
  216.  
  217.     if (graphicsContext == NULL) {
  218.         gcValues.foreground = BlackPixelOfScreen(screen);
  219.         gcValues.background = WhitePixelOfScreen(screen);
  220.         graphicsContext = XCreateGC(display, window,
  221.                 GCForeground | GCBackground, &gcValues);
  222.     }
  223.  
  224.     /*
  225.      * Get the dragged icon from the dropDraw area
  226.      */
  227.  
  228.     XtVaGetValues(dropDraw, XmNuserData, &iconPtr, NULL);
  229.  
  230.     if (iconPtr == NULL) {
  231.         return;
  232.     }
  233.  
  234.     /*
  235.      * Set clip mask and coordinates for this icon in the graphics context
  236.      */
  237.  
  238.     gcValues.clip_mask = iconPtr->mask;
  239.     gcValues.clip_x_origin = iconPtr->icon.x;
  240.     gcValues.clip_y_origin = iconPtr->icon.y;
  241.     XChangeGC(display, graphicsContext,
  242.             GCClipMask | GCClipXOrigin | GCClipYOrigin,
  243.             &gcValues);
  244.  
  245.     /*
  246.      * Reconstitute the icon after Motif melts it
  247.      */
  248.  
  249.     for (expandWidth = expandHeight = 0;
  250.          expandWidth < (int)iconPtr->icon.width &&
  251.              expandHeight < (int)iconPtr->icon.height;
  252.          expandWidth += 2, expandHeight += 2) {
  253.  
  254.         sourceX = ((int)iconPtr->icon.width - expandWidth)/2;
  255.         sourceY = ((int)iconPtr->icon.height - expandHeight)/2;
  256.  
  257.         XCopyPlane(display, iconPtr->bitmap, window, graphicsContext,
  258.             sourceX, sourceY, expandWidth, expandHeight,
  259.             iconPtr->icon.x + sourceX, iconPtr->icon.y + sourceY,
  260.             1L);
  261.  
  262.         _XmMicroSleep(25000L);
  263.         XFlush(display);
  264.     }
  265. }
  266.  
  267. /*
  268.  * demoDrawExposeCallback
  269.  *
  270.  * Draws all the icons associated with the drawing area.
  271.  */
  272. void
  273. demoDrawExposeCallback(
  274.     Widget        widget,
  275.     XtPointer    clientData,
  276.     XtPointer    callData)
  277. {
  278.     IconInfo       *iconPtr;
  279.  
  280.     XtVaGetValues(widget, XmNuserData, &iconPtr, NULL);
  281.  
  282.     while (iconPtr != NULL) {
  283.         IconDraw(widget, iconPtr);
  284.         iconPtr = iconPtr->next;
  285.     }
  286. }
  287.  
  288. /*
  289.  * demoDropSetup
  290.  *
  291.  * Registers draw area to accept drops of files or buffers such as appointments.
  292.  */
  293. void
  294. demoDropSetup(
  295.     Widget        dropDraw)
  296. {
  297.     static XtCallbackRec transferCBRec[] = { {demoTransferCallback, NULL},
  298.                              {NULL, NULL} };
  299.     static XtCallbackRec animateCBRec[] = { {demoDrawAnimateCallback, NULL},
  300.                             {NULL, NULL} };
  301.  
  302.     animateCBRec[0].closure = (XtPointer)dropDraw;
  303.  
  304.     DtDndVaDropRegister(dropDraw,
  305.         DtDND_FILENAME_TRANSFER | DtDND_BUFFER_TRANSFER,
  306.         XmDROP_COPY | XmDROP_MOVE, transferCBRec, 
  307.         DtNdropAnimateCallback,        animateCBRec,
  308.         DtNtextIsBuffer,         True,
  309.         DtNpreserveRegistration,     False,
  310.         NULL);
  311. }
  312.  
  313. /*
  314.  * demoTransferCallback
  315.  *
  316.  * Called when something is dropped on the drawing area drop site. If the
  317.  * transfer protocol is DtDND_FILENAME_TRANSFER or DtDND_BUFFER_TRANSFER
  318.  * the fileTransferCallback and apptTransferCallback are called respectively.
  319.  */
  320. void
  321. demoTransferCallback(
  322.         Widget          widget,
  323.         XtPointer       clientData,
  324.         XtPointer       callData)
  325. {
  326.         DtDndTransferCallbackStruct *transferInfo =
  327.                                 (DtDndTransferCallbackStruct *) callData;
  328.  
  329.     if (transferInfo == NULL) {
  330.         return;
  331.     }
  332.  
  333.     switch (transferInfo->dropData->protocol) {
  334.  
  335.     case DtDND_FILENAME_TRANSFER:
  336.         fileTransferCallback(widget, clientData, callData);
  337.         break;
  338.     case DtDND_BUFFER_TRANSFER:
  339.         apptTransferCallback(widget, clientData, callData);
  340.         break;
  341.     }
  342. }
  343.  
  344.  /*************************************************************************
  345.  *
  346.  *    Demo Client Creation
  347.  *
  348.  *************************************************************************/
  349.  
  350. /*
  351.  * Fallback resources are used if app-defaults file is not found
  352.  */
  353.  
  354. String    fallbackResources[] = {
  355.     "title:                    CDE Drag & Drop Demo",
  356.  
  357.     "*outerRowColumn.orientation:        VERTICAL",
  358.     "*outerRowColumn.spacing:        15",
  359.     "*outerRowColumn.marginHeight:        15",
  360.     "*outerRowColumn.marginWidth:        15",
  361.  
  362.     "*upperRowColumn.orientation:        HORIZONTAL",
  363.     "*upperRowColumn.packing:        PACK_COLUMN",
  364.     "*upperRowColumn.spacing:        15",
  365.     "*upperRowColumn.marginHeight:        0",
  366.     "*upperRowColumn.marginWidth:        0",
  367.  
  368.     "*fileDraw.height:            175",
  369.     "*fileDraw.resizePolicy:        RESIZE_NONE",
  370.  
  371.     "*fruitList.listSizePolicy:        CONSTANT",
  372.     "*fruitList.scrollBarDisplayPolicy:    STATIC",
  373.     "*fruitList.selectionPolicy:        MULTIPLE_SELECT",
  374.  
  375.     "*apptList.listSizePolicy:        CONSTANT",
  376.     "*apptList.scrollBarDisplayPolicy:    STATIC",
  377.     "*apptList.selectionPolicy:        MULTIPLE_SELECT",
  378.  
  379.     "*textRowColumn.orientation:        HORIZONTAL",
  380.     "*textRowColumn.packing:        PACK_TIGHT",
  381.     "*textRowColumn*textLabel.labelString: Name:",
  382.     "*textRowColumn*textField.width:    550",
  383.     "*textRowColumn.marginWidth:        0",
  384.  
  385.     "*dropDraw.height:            100",
  386.     "*dropDraw.resizePolicy:        RESIZE_NONE",
  387.     NULL
  388. };
  389.  
  390. /*
  391.  * demoCreateDropSite
  392.  *
  393.  * Creates the drawing area at the bottom of the demo which is used as a drop
  394.  * site for files and appointments.
  395.  */
  396. Widget
  397. demoCreateDropSite(
  398.     Widget        parent)
  399. {
  400.     Widget        dropFrame,
  401.             dropDraw;
  402.  
  403.     dropFrame = XtVaCreateManagedWidget("dropFrame",
  404.         xmFrameWidgetClass, parent,
  405.         NULL);
  406.  
  407.     dropDraw = XtVaCreateManagedWidget("dropDraw",
  408.         xmDrawingAreaWidgetClass, dropFrame,
  409.         NULL);
  410.  
  411.     XtAddCallback(dropDraw,
  412.         XmNexposeCallback, demoDrawExposeCallback,
  413.         NULL);
  414.  
  415.     return dropDraw;
  416. }
  417.  
  418. /*
  419.  * main
  420.  *
  421.  * Create widgets for the demo program. Call protocol-specific functions
  422.  * to initialize the drag sources and drop sites.
  423.  */
  424. void
  425. main(
  426.     int         argc,
  427.     String           *argv)
  428. {
  429.     Widget        outerRowColumn,
  430.             upperRowColumn,
  431.             textDragSource,
  432.             fileDragSource,
  433.             apptDragSource,
  434.             separator,
  435.             textDropSite,
  436.             drawDropSite;
  437.  
  438.     /*
  439.      * Create basic widgets and layout widgets
  440.      */
  441.  
  442.     XtSetLanguageProc(NULL, NULL, NULL);
  443.  
  444.     topLevel = XtAppInitialize(&appContext, "Dtdnddemo", 
  445.         (XrmOptionDescList)NULL, 0, &argc, argv, 
  446.         fallbackResources, (ArgList)NULL, 0);
  447.  
  448.     DtAppInitialize(appContext, XtDisplay(topLevel), 
  449.         topLevel, argv[0], "Dtdnddemo"); 
  450.  
  451.     outerRowColumn = XtVaCreateManagedWidget("outerRowColumn",
  452.         xmRowColumnWidgetClass, topLevel,
  453.         NULL);
  454.  
  455.     upperRowColumn = XtVaCreateManagedWidget("upperRowColumn",
  456.         xmRowColumnWidgetClass, outerRowColumn,
  457.         NULL);
  458.  
  459.     /*
  460.      * Create the drag sources
  461.      */
  462.  
  463.     textDragSource = textCreateDragSource(upperRowColumn);
  464.     fileDragSource = fileCreateDragSource(upperRowColumn);
  465.     apptDragSource = apptCreateDragSource(upperRowColumn);
  466.  
  467.     /*
  468.      * Create a line separating the drag sources from the drop sites
  469.      */
  470.  
  471.     separator = XtVaCreateManagedWidget("separator",
  472.         xmSeparatorWidgetClass, outerRowColumn,
  473.         NULL);
  474.     
  475.     /*
  476.      * Create the drop sites
  477.      */
  478.  
  479.     textDropSite = textCreateDropSite(outerRowColumn);
  480.     drawDropSite = demoCreateDropSite(outerRowColumn);
  481.  
  482.     /*
  483.      * Realize the widget tree
  484.      */
  485.  
  486.     XtRealizeWidget(topLevel);
  487.  
  488.     /*
  489.      * Load the data typing database which is used to get icons
  490.      */
  491.  
  492.     DtDtsLoadDataTypes();
  493.  
  494.     /*
  495.      * Set up the drag sources
  496.      */
  497.  
  498.     textDragSetup(textDragSource);
  499.     fileDragSetup(fileDragSource);
  500.     apptDragSetup(apptDragSource);
  501.  
  502.     /*
  503.      * Set up the drop sites
  504.      */
  505.  
  506.     textDropSetup(textDropSite);
  507.     demoDropSetup(drawDropSite);
  508.  
  509.     /*
  510.      * Start the event processing loop
  511.      */
  512.  
  513.     XtAppMainLoop(appContext);
  514. }
  515.